package combat.log;

import combat.events.Event;
import combat.unit.Actor;

public class CombatLogEntry {
  private String[] parameters;
  private String entry;
  private String time;
  private Event event;
  private Actor source;
  private Actor dest;
  private String[] eventParams;

  /**
   * Creates a combat log entry using the given line. This is assumed to be one line, and
   * follow the format used by World of Warcraft, post 2.4 patch.
   * @param line the line to parse.
   */
  public CombatLogEntry(String line) {
    entry = line;
    parse();
  }

  /**
   * Outputs the line as a String, which should be the same as the String used to create
   * this object.
   */
  public String toString() {
    String out = time + "  ";
    out += event + ",";
    out += source + ",";
    out += dest + ",";
    for (int i = 0; i < eventParams.length; i++) {
      out += eventParams[i];
      if (i + 1 < eventParams.length)
        out += ",";
    }
    return out;
  }

  /**
   * Parses the line this object represents. The line is loaded into an array of objects.
   */
  private void parse() {
    String[] t = entry.split("  ");
    String[] params = t[1].split(",");
    parameters = new String[1 + params.length];
    parameters[0] = t[0];
    time = t[0];
    for (int i = 0; i < params.length; i++) {
      parameters[i + 1] = params[i];
    }
    event = new Event(params[0]);
    source = new Actor(params[1], params[2], params[3]);
    dest = new Actor(params[4], params[5], params[6]);
    eventParams = new String[params.length - 7];
    for (int i = 7; i < params.length; i++)
      eventParams[i - 7] = params[i];
  }
}
